<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if !IE]><!-->
<html lang="zh">
<!--<![endif]-->
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
%>

<head>
    <meta charset="utf-8" />
    <title>保险代理后台管理系统</title>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />
    <!-- ================== BEGIN BASE CSS STYLE ================== -->
    <jsp:include page="../common/header.jsp"></jsp:include>
    <%@include file="../common/taglib.jsp"%>
    <!-- ================== END BASE CSS STYLE ================== -->

    <!-- ================== 单独作用于本页表格td ================== -->
    <style>
        .form-control{
        	width:200px;
        }
        .dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 1000;
    display: none;
    float: left;
    min-width: 160px;
    padding: 5px 0;
    margin: 2px 0 0;
    font-size: 14px;
    text-align: left;
    list-style: none;
    background-color: #fff;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    border: 1px solid #ccc;
    border: 1px solid rgba(0,0,0,.15);
    border-radius: 4px;
}
        tr td:first-child, tr th:first-child{
        	text-align:center;
        }
    </style>
    <!-- ================== 单独作用于本页表格td ================== -->

</head>
<body>
<!-- begin #page-loader -->
<div id="page-loader" class="fade in"><span class="spinner"></span></div>
<!-- end #page-loader -->

<!-- begin #page-container -->
<div id="page-container" class="fade page-sidebar-fixed page-header-fixed">
    <jsp:include page="../common/top.jsp"/>

    <!-- begin #sidebar -->
    <jsp:include page="../common/sitebar.jsp"/>
    <div class="sidebar-bg"></div>
    <!-- end #sidebar -->

    <!-- begin #content -->
    <div id="content" class="content">       
        <div class="row">
            <!-- begin col-12 -->
            <div class="col-md-12">
                <!-- begin panel -->
                <div class="panel panel-inverse">
                    <section class="pad-top-10 pad-bottom-20">
                        <ul class="nav nav-tabs" >
                            <li class="active"><a href="#tab1" data-toggle="tab">外部用户</a></li>
                            <li><a href="#tab2" id="internalUser" data-toggle="tab">内部用户</a></li>
                            <li><a href="#tab3" id="addInternalUser" data-toggle="tab">新建内部用户</a></li>
                            <li><a href="#tab4" id="tabEdit" data-toggle="tab" class="hidden">编辑内部用户</a></li>
                        </ul>
                        <div class="table-responsive">
                        <div class="form-wrap mar-bottom-0">
                            <div class="tab-content">
                            
                               <!-- 外部用户列表 -->
                                 <div id="tab1" class="tab-pane fade active in">
                                <section>
                                     <form class="form-inline">
                                          <div class="form-group" style="position: relative;">
								    			<label>渠道：</label>
								    			<input type="hidden" name="channelId" id="channelId">
                                				<input type="text" id="channels" name="channel" data-toggle="dropdown"  class="form-control m-r-20 dropdown-toggle" placeholder="渠道" />
                                				<ul id="chs" class="dropdown-menu hidden" style="max-height: 200px;overflow: auto;margin-left: 48px;width:193px;">
                                				</ul>
								  		  </div>
								  		  
								  		  <div class="form-group">
								  		  		<label>状态：</label>
								  		  		<select id="status2" class="form-control m-r-10" style="list-style-type: none;width:100px;">
                                                        <option value="">请选择</option>
                                                        <option value="0">使用中</option>
                                                        <option value="1">已禁用</option>
                                                </select>
								  		  </div>
								  		<button type="button" class="btn btn-primary" onclick="searchExternal()" style="position: relative;">查询</button>
                                     </form>

                                    <table id="external" class="table table-striped table-bordered"  style= "word-break:break-all;word-wrap:break-word">
                                        <thead>
                                        <tr>
                                            <th width="80">编号</th>
                                            <th>登录名</th>
                                            <th>渠道</th>
                                            <th>联系人</th>
                                            <th>联系电话</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        
                                        </tbody>
                                    </table>
                                </section>
                            </div>
                            
                            
                            
                            
                            
                            <!-- 内部用户列表 -->
                                <div id="tab2" class="tab-pane fade">
                                    <section>
                                       <form class="form-inline">
                                       		<div class="form-group">
                                       			<label>部门：</label>
                                       			<select class="form-control m-r-20" id="office_id" style="list-style-type: none;width:100px;">
                                                		<option value="">请选择</option>
                                                		<c:forEach items="${officeList}" var="office">
                                                    			<option value="${office.tid}">${office.officeName}</option>
                                               			</c:forEach>
                                            	</select>
                                       		</div>
                                       		<div class="form-group">
                                       				<label>姓名：</label>
                                       				<input type="text" id="username" class="form-control m-r-20" style="position: relative;width: 100px;">
                                       		</div>
                                       		<div class="form-group">
                                       				<label>状态：</label>
                                       				<select id="status" class="form-control m-r-10" style="list-style-type: none;width:100px;">
                                                          <option value="">请选择</option>
                                                          <option value="0">使用中</option>
                                                          <option value="1">已禁用</option>
                                                    </select>
                                       		</div>
                                       		<button type="button" class="btn btn-primary" onclick="search()" style="position: relative;">查询</button>
                                       </form>
                                    
                                    
                                        <table id="gadtable" class="table table-striped table-bordered"  style="word-break:break-all;word-wrap:break-word">
                                            <thead>
                                            <tr>
                                                <th>编号</th>
                                                <th>部门</th>
                                                <th>角色</th>
                                                <th>姓名</th>
                                                <th>工号</th>
                                                <th>手机号</th>
                                                <th>状态</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                        </table>
                                    </section>
                                </div>
                                
                                <!-- 新建内部用户页 -->
                                  <div id="tab3" class="tab-pane fade">
                                    <form class="form-horizontal form-bordered" action="<%=path %>/sysuser/add" method="post" id="form" data-parsley-validate="true">
                                    	<input id="message" type="hidden" value="${message }">
                                           <div class="form-group">
                                                    <label class="col-md-1 control-label" style="width: 110px;">部门：</label>
                                                    <div class="col-md-2">
                                            					<select style="list-style-type: none;" class="form-control" id="departmentId" name="officeId" onchange="toselectRole(this.options[this.options.selectedIndex].value)" data-parsley-required="true" data-parsley-required-message="部门不能为空">
                                                						<option value="">请选择</option>
                                                				<c:choose>
                                                					<c:when test="${result==1 }">
                                                						<c:forEach items="${officeList}" var="office">
                                                    						<option value="${office.tid}">${office.officeName}</option>
                                               						 	</c:forEach>
                                                					</c:when>
                                                					<c:otherwise>
                                                    						<option value="${office.officeId}">${office.officeName}</option>
                                                					</c:otherwise>
                                                				</c:choose>
                                            					</select>
                                                    </div>
                                           </div>
                                          
                                          
                                           <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">姓名：</label>
                                                    <div class="col-md-6 col-sm-6">
                                                        <input type="text" name="username" id="username2" maxlength="24" class="form-control" data-parsley-required="true" data-parsley-required-message="姓名不能为空"/>
                                                    </div>
                                           </div>
                                           
                                           <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">手机号：</label>
                                                    <div class="col-md-6 col-sm-6">
                                                        <input type="text" name="phone" maxlength="11" class="form-control" data-parsley-required="true" data-parsley-required-message="手机号不能为空" data-parsley-phone="true" data-parsley-phone-message="请输入正确手机号"/>
                                                    </div>
                                           </div>
                                                
                                           <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">工号(登录名)：</label>
                                             		<div class="col-md-6 col-sm-6">
                                                        <input type="text" name="jobNumber" data-parsley-required="true" data-parsley-only="true" data-parsley-only-message="该工号已被使用" data-parsley-required-message="工号不能为空" maxlength="6" id="jobNumber" class="form-control" data-parsley-number="true" data-parsley-number-message="请输入数字"/>
                                                    </div>
                                           </div>
                                                
                                           <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">设置密码：</label>
                                                    <div class="col-md-6 col-sm-6">
                                                        <input id="user_password" data-parsley-length="[6,16]" data-parsley-length-message="请输入6-16字符以内" data-parsley-nzcheck="true" data-parsley-nzcheck-message="请输入字母、数字或符号两种组合" data-parsley-required="true" data-parsley-required-message="密码不能为空" type="password" class="form-control"/>
                                                    </div>
                                           </div>
                                                
                                           <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">确认密码：</label>
                                                    <div class="col-md-6 col-sm-6">
                                                        <input type="password"id="password2" data-parsley-required="true" data-parsley-required-message="密码不能为空" data-parsley-equalto="#user_password" data-parsley-equalto-message="两次输入结果不一致" class="form-control"/>
                                                        <input type="hidden" name="password" id="passwords">
                                                    </div>
                                           </div>
                                                
                                           <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">分配角色：</label>
                                                    <div class="col-md-6 col-sm-6" id="role_id">  
                                                         <!-- <label class="checkbox-inline parsley-success"><input type="checkbox" checked="checked" data-parsley-required="true" data-parsley-required-message="角色不能为空" name="roleId" value="5" data-parsley-multiple="roleId" data-parsley-id="36">qqqq</label><label class="checkbox-inline"><input type="checkbox" data-parsley-required="true" data-parsley-required-message="角色不能为空" name="roleId" value="6" checked="checked" data-parsley-multiple="roleId">wwww</label><label class="checkbox-inline"><input type="checkbox" data-parsley-required="true" data-parsley-required-message="角色不能为空" name="roleId" value="7" data-parsley-multiple="roleId">风控复核员</label><label class="checkbox-inline"><input type="checkbox" data-parsley-required="true" data-parsley-required-message="角色不能为空" name="roleId" value="8" data-parsley-multiple="roleId">风控负责人</label> -->                                               
                                                    </div>
                                           </div>
                                           
                                           <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">&nbsp;</label>
                                                    <div class="col-md-6 col-sm-6">                                                      
                                                        <button type="button" id="showTab" class="btn btn-primary m-r-10">&nbsp;提交&nbsp;</button>
                                        				<a href="#modal-dialog5" class="btn btn-default" data-toggle='modal'>&nbsp;取消&nbsp;</a>
                                                    </div>
                                                    <input type="hidden" name="roleName" id="roleName">
                                           </div>
                                    </form>
                                </div>
                                
                               
                               
                               <!-- 编辑内部用户页 -->
                               <div id="tab4" class="tab-pane fade">
                                 <section>
                                    <form id="form2" class="form-horizontal form-bordered" action="<%=path %>/sysuser/add" method="post" data-parsley-validate="true">
                                    	 <input type="hidden" id="userId" name="tid">
                                         <div class="form-group">
                                                <label class="col-md-1 control-label" style="width: 110px;">部门：</label>
                                                    <div class="col-md-2">
                                                    	<select style="list-style-type: none;" name="officeId" class="form-control" id="officeName1" onchange="toselectRoles(this.options[this.options.selectedIndex].value)" data-parsley-required="true" data-parsley-required-message="部门不能为空">
                                                						<option value="">请选择</option>
                                                					 <c:forEach items="${officeList}" var="office">
                                                    					<option value="${office.tid}">${office.officeName}</option>
                                               						 </c:forEach>
                                            			</select>
                                                    </div>
                                         </div>
                                         
                                         <div class="form-group">
                                                <label class="control-label col-md-4 col-sm-4" style="width: 110px;">姓名：</label>
                                                    <div class="col-md-6 col-sm-6">
                                                        <input type="text" name="username" id="username1" maxlength="24" class="form-control" data-parsley-required="true" data-parsley-required-message="姓名不能为空"/>
                                                    </div>
                                         </div>
                                         
                                         <div class="form-group">
                                                <label class="control-label col-md-4 col-sm-4" style="width: 110px;">手机号：</label>
                                                    <div class="col-md-6 col-sm-6">
                                                        <input type="text" name="phone" id="phone1" maxlength="11" class="form-control" data-parsley-required="true" data-parsley-required-message="手机号不能为空" data-parsley-phone="true" data-parsley-phone-message="请输入正确手机号"/>
                                                    </div>
                                         </div>
                                         
                                         <div class="form-group">
                                                <label class="control-label col-md-4 col-sm-4" style="width: 110px;">工号/登录名：</label>
                                             		<div class="col-md-6 col-sm-6">
                                                        <input type="text" id="jobNumber1" class="form-control" disabled="ture"/>
                                                    </div>
                                         </div>
                                               
                                         <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">分配角色：</label>
                                                    <div class="col-md-6 col-sm-6" id="roles">
                                                      
                                                    </div>
                                                    <input type="hidden" name="roleName" id="roleName2">
                                         </div>
                                              
                                         <div class="form-group">
                                                    <label class="control-label col-md-4 col-sm-4" style="width: 110px;">&nbsp;</label>
                                                    <div class="col-md-6 col-sm-6">                                                      
                                                        <button type="button" id="showTab2" class="btn btn-primary m-r-10">&nbsp;提交&nbsp;</button>
                                        				<a href="#modal-dialog5" data-toggle='modal' class="btn btn-default">&nbsp;取消&nbsp;</a>
                                                    </div>
                                         </div>  
                                    </form>
								  </section>
                                </div>
                                </div>
                              </div>
                            </div>
                    </section>
                </div>
                <!-- end panel -->
            </div>
            <!-- end col-12 -->
        </div>
        <!-- end row -->
    </div>

<!-- 外部用户重置密码弹框 -->
<div class="modal fade" id="modal-dialog" style="display: none;">
								<div class="modal-dialog modal-sm">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											<h4 class="modal-title">重置密码</h4>
										</div>
										<div class="modal-body">
											确认重置密码
										</div>
										<input type="hidden" id="externalId" value="">
										<input type="hidden" id="username4" value="">
										<div class="modal-footer">
											<a href="javascript:;" onclick="toReset()" class="btn btn-primary">确定</a>
											<a href="javascript:;" class="btn btn-default" data-dismiss="modal">取消</a>
										</div>
									</div>
								</div>
							</div>
							
							
<!-- 内部用户重置密码弹框 -->
<div class="modal fade" id="modal-dialog2" style="display: none;">
								<div class="modal-dialog modal-sm">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											<h4 class="modal-title">重置密码</h4>
										</div>
										<div class="modal-body">
											确认重置密码
										</div>
										<input type="hidden" id="internalId" value="">
										<input type="hidden" id="username3" value="">
										<div class="modal-footer">
											<a href="javascript:;" onclick="toResetInternal()" class="btn btn-primary">确定</a>
											<a href="javascript:;" class="btn btn-default" data-dismiss="modal">取消</a>
										</div>
									</div>
								</div>
							</div>
							
							
<!-- 禁用提示弹框 -->
<div class="modal fade" id="modal-dialog3" style="display: none;">
								<div class="modal-dialog modal-sm">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											<h4 class="modal-title">禁用用户</h4>
										</div>
										<div class="modal-body">
											确认禁用该用户?
										</div>
										<input type="hidden" id="forbiddenId" value="">
										<div class="modal-footer">
											<a href="javascript:;" onclick="updateStatus2()" class="btn btn-primary">确定</a>
											<a href="javascript:;" class="btn btn-default" data-dismiss="modal">取消</a>
										</div>
									</div>
								</div>
							</div>
							
<!-- 启用提示弹框 -->
<div class="modal fade" id="modal-dialog4" style="display: none;">
								<div class="modal-dialog modal-sm">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											<h4 class="modal-title">启用用户</h4>
										</div>
										<div class="modal-body">
											确认解禁该用户?
										</div>
										<input type="hidden" id="useId" value="">
										<div class="modal-footer">
											<a href="javascript:;" onclick="updateStatus()" class="btn btn-primary">确定</a>
											<a href="javascript:;" class="btn btn-default" data-dismiss="modal">取消</a>
										</div>
									</div>
								</div>
							</div>
							
							
<!-- 提示框 -->
<div class="modal fade" id="modal-dialog5">
								<div class="modal-dialog modal-sm">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											<h4 class="modal-title">取消内容</h4>
										</div>
										<div class="modal-body">
											当前填写内容未提交,确认取消?
										</div>
										<div class="modal-footer">
											<a href="javascript:;" onclick="tolist()" class="btn btn-primary">确定</a>
											<a href="javascript:;" class="btn btn-default" data-dismiss="modal">取消</a>
										</div>
									</div>
								</div>
							</div>

<!-- 提交成功弹框 -->							
<div class="modal fade" id="modal-dialog6">
								<div class="modal-dialog modal-sm">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											<h4 class="modal-title">内容</h4>
										</div>
										<div class="modal-body">
											提交成功
										</div>
									</div>
								</div>
							</div>
			
<!-- 提交失败弹框 -->				
<div class="modal fade" id="modal-dialog7">
								<div class="modal-dialog modal-sm">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											<h4 class="modal-title">内容</h4>
										</div>
										<div class="modal-body">
											提交失败,请联系管理员
										</div>
									</div>
								</div>
							</div>
    <!-- end #content -->
</div>
<!-- end page container -->
<jsp:include page="../common/footer.jsp"/>

<!-- ================== END PAGE LEVEL JS ================== -->
<script src="${ctx }/static/js/crypto-js.js"></script>
<script>
    $(document).ready(function() {
        App.init();
        TableManageFixedHeader.init();
    });
    
    window.ParsleyValidator.addValidator('only',function (jobNumber){
    	var result = true;
    	if(jobNumber!="" && jobNumber!=null){
    		$.ajax({
    			type:"post",
    			url:"<%=path%>/sysuser/onlyJobNumber",
    			async:false,
    			data:{jobNumber:jobNumber},
    			success:function (msg){
    				if(msg.success!=true){
    					result = false;
    				}
    			}
    		});
    	}
    	return result;
    },32);
    
    window.ParsleyValidator.addValidator('phone', function(value) {
        if(value!= ""){
            return /^1\d{10}$/.test(value);
        }
        return true;
    },32);
    
    window.ParsleyValidator.addValidator('number', function(value) {
        if(value!= ""){
            return /^[0-9]*$/.test(value)
        }
        return true;
    },32);
    
    window.Parsley.addValidator('nzcheck', function(value) {
        if(value!= ""){
        	return /^(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-zA-Z]|[0-9])*$/.test(value)
        }
        return true;
    },32);
    
    
    //查询所有渠道名称
    $("#channels").keyup(function () {
            var name = $("#channels").val();
            $("#chs").addClass("hidden");
            if(name!=null&&name!=""){
                $("#chs").html("");
                $("#channelId").val("");
                $.ajax({
                    url:"/channel/findAllChannelName/"+name,
                    data:{},
                    type:"get",
                    success:function (data) {
                        if(data.success){
                            $("#chs").removeClass("hidden");
                            var channelIds = "";
                            var chHtml = "";
                            for (var i in data.result){
                                if(data.result[i].companyRegisterName!=null){
                                    chHtml += '<li value='+data.result[i].tid+'><a href="javascript:;">'+data.result[i].companyRegisterName+'</a></li>';
                                }else{
                                    chHtml += '<li value='+data.result[i].tid+'><a href="javascript:;">'+data.result[i].contact.name+'</a></li>';
                                }
                                channelIds += "," + data.result[i].tid;
                            }
                            if(channelIds!=null&&channelIds!=""){
                                channelIds = channelIds.substring(1,channelIds.length);
                                $("#chs").parent().addClass("open");
                            }else{
                                channelIds = -1;
                                $("#chs").parent().removeClass("open");
                            }
                            $("#chs").html(chHtml);
                            $("#chs").parent().addClass("open");
                            $("#channelId").val(channelIds);
                            $("#chs li").click(function () {
                                $("#channels").val($(this).text());
                                $("#channelId").val($(this).val());
                                $("#chs").parent().removeClass("open");
                            })

                        }else{
                            $("#chs").parent().removeClass("open");
                            $("#channelId").val("");
                            $("#channelId").val(-1);
                        }
                    },
                    dataType:"json"
                })
            }else{
                $("#chs").parent().removeClass("open");
                $("#channelId").val("");
                $("#chs li").remove();
            }
        })
    
    
    
    function tolist(){
    	$("#tabEdit").addClass("hidden");
    	$('#modal-dialog5').modal('hide')
    	$("#internalUser").trigger("click");
     	table.ajax.reload();
     	$("#form").find("ul").addClass("hidden");
     	$("#form").find("input").val("").removeClass("parsley-error");
     	$("#form").find("select").val("").removeClass("parsley-error");
     	$("#form").find("input").val("").removeClass("parsley-success");
     	$("#form").find("select").val("").removeClass("parsley-success");
     	$("#form2").find("input").removeClass("parsley-success");
     	$("#form2").find("ul").addClass("hidden");
     	$("#form2").find("input").removeClass("parsley-error");
    }

    
    //AES-128-CBC加密模式，key需要为16位，key和iv可以一样
    function encrypt(data) {
        var key  = CryptoJS.enc.Latin1.parse('0415f531c6e4e17b');
        var iv   = CryptoJS.enc.Latin1.parse('0415f531c6e4e17b');
        return CryptoJS.AES.encrypt(data, key, {iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding}).toString();
    }
    
    function encryptInput() {
        //获取用户输入值
        //var password = $("#user_password").val();
        var password2 = $("#password2").val();
        //加密用户名和密码
        //password = encrypt(password);
        password2 = encrypt(password2);
        //$("#user_password").val(password);
        $("#passwords").val(password2);
    }
    
    $("#showTab").click(function (){
    	$('#form').parsley().whenValidate({
    	      
    	    }).done(function() {
    	    	encryptInput();
    	    	var checkboxValue= new Array();  
    	        var checkboxText= new Array();  
    	        var checkboxStr=document.getElementsByName("roleId");    
    	        for(var i=0; i<checkboxStr.length; i++){  
    	         if(checkboxStr[i].checked){  
    	             checkboxValue.push(checkboxStr[i].value);  
    	             checkboxText.push(checkboxStr[i].nextSibling.nodeValue);
    	          } 
    	        }
    	        $("#roleName").val(checkboxText);
    	    	$.ajax({
    	    		type:"post",
    	    		url:"<%=path %>/sysuser/add",
    	    		data:$("#form").serialize(),
    	    		success:function (msg){
    	    			if(msg.success==true){
    	    			$("#modal-dialog6").modal().delay(1000).fadeOut();
    	    	     	$("#internalUser").trigger("click");
    	    	     	table.ajax.reload();
    	    	     	$("#form").find("input").val("").removeClass("parsley-success");
    	    	     	$("#form").find("select").val("").removeClass("parsley-success");
    	    		  }else{
    	    			$("#modal-dialog7").modal().delay(1000).fadeOut();
      	    	     	$("#internalUser").trigger("click");
      	    	     	table.ajax.reload();
      	    	     	$("#form").find("input").val("").removeClass("parsley-success");
      	    	     	$("#form").find("select").val("").removeClass("parsley-success");
    	    		  }
    	    		}
    	    	});
    	 });
    });
    
       
    $("#showTab2").click(function (){
    	$('#form2').parsley().whenValidate({
  	      
	    }).done(function() {
	    	var checkboxValue= new Array();  
	        var checkboxText= new Array();  
	        var checkboxStr=document.getElementsByName("roleId");    
	        for(var i=0; i<checkboxStr.length; i++){  
	         if(checkboxStr[i].checked){  
	             checkboxValue.push(checkboxStr[i].value);  
	             checkboxText.push(checkboxStr[i].nextSibling.nodeValue);
	          } 
	        }
	        $("#roleName2").val(checkboxText);
	    	 $.ajax({
	    		type:"post",
	    		url:"<%=path %>/sysuser/add",
	    		data:$("#form2").serialize(),
	    		success:function (msg){
	    			if(msg.success==true){
	    				$("#tabEdit").addClass("hidden");
	    	     		$("#internalUser").trigger("click");
	    	     		table.ajax.reload();
	    	     		$("#form2").find("input").removeClass("parsley-success");
	    		  }
	    		}
	    	}); 
	 });
    })
    
     function toReset(){
    	var id = $("#externalId").val();
    	var message = "1";
    	 $.ajax({
    		type:"post",
    		url:"<%=path%>/sysuser/resetPassword",
    		data:{id:id,message:message},
    		success:function (msg){
    			if(msg.success==true){
   				 $("#modal-dialog").modal("hide");
   				 externalTable.ajax.reload();
   			 }
    		}
    	});
    }
    
     
     function toResetInternal(){
    	 var id = $("#internalId").val();
    	 var jobNumber = $("#username3").val();
    	 var message = "0";
    	 $.ajax({
    		 type:"post",
    		 url:"<%=path%>/sysuser/resetPassword",
    		 data:{id:id,message:message,jobNumber:jobNumber},
    		 success:function (msg){
    			 if(msg.success==true){
    				 $("#modal-dialog2").modal("hide");
     				 table.ajax.reload();
    			 }
    		 }
    	 });
     }

     
    //去往编辑页面
    function toedit(id){
         $.ajax({
        	type:"post",
        	url:"<%=path%>/sysuser/toedit/"+id,
        	success:function (msg){
        		$("#userId").val(msg.tid);
        		$("#username1").val(msg.username);
        		$("#officeName1").find("option[value='"+msg.officeId+"']").attr("selected",true);
        		$("#phone1").val(msg.phone);
        		$("#jobNumber1").val(msg.jobNumber);
        		toselectRoles(msg.officeId);
        		var arr=msg.roleId.split(',');
        		$("#roles label [name='roleId']").val(arr);
        		$("#tabEdit").removeClass("hidden").trigger("click");
        	}
        });
    }


    function updateStatus(){
    	var tid = $("#useId").val();
    	var status = "0";
    	$.ajax({
    		type:"post",
    		url:"<%=path%>/sysuser/updateStatus",
    		data:{tid:tid,status:status},
    		dataType:"json",
    		success:function (msg){
    			if (msg.success == true) {
    				$("#modal-dialog4").modal("hide");
    				table.ajax.reload();
    				externalTable.ajax.reload();
                }
    		}
    	});
    } 
    
    
    function updateStatus2(){
    	var tid = $("#forbiddenId").val();
    	var status = "1";
    	  $.ajax({
    		type:"post",
    		url:"<%=path%>/sysuser/updateStatus",
    		data:{tid:tid,status:status},
    		dataType:"json",
    		success:function (msg){
    			if (msg.success == true) {
    				$("#modal-dialog3").modal("hide");
    				table.ajax.reload();
    				externalTable.ajax.reload();
                }
    		}
    	});
    }
    
    function getId(id,jobNumber){
    	document.getElementById("forbiddenId").value=id;
    	document.getElementById("useId").value=id;
    	document.getElementById("internalId").value=id;
    	document.getElementById("externalId").value=id;
    	document.getElementById("username3").value=jobNumber;
    }
    
    function toselectRole(officeId){
   		 $("#role_id").empty();
    	var add="";
    		$.ajax({
    			type:"post",
    			url:"<%=path%>/sysuser/toSelectRole",
    			data:{officeId:officeId},
    			async:false,
    			success:function (data){
    				var add="";
    				for(var i=0;i<data.length;i++){
    					add+="<label class='checkbox-inline'><input type='checkbox' data-parsley-required='true' data-parsley-required-message='角色不能为空' name='roleId' value='"+data[i].tid+"'>"+data[i].name+"</label>";
    				}
    				$("#role_id").append(add);
    			}
    		});
    }
    
    
    function toselectRoles(officeId){
    	var add="";
    	$("#roles").empty();
    		$.ajax({
    			type:"post",
    			url:"<%=path%>/sysuser/toSelectRole",
    			data:{officeId:officeId},
    			async:false,
    			success:function (data){
    				var add="";
    				for(var i=0;i<data.length;i++){
    					add+="<label class='checkbox-inline'><input type='checkbox' data-parsley-required='true' data-parsley-required-message='角色不能为空' name='roleId' value='"+data[i].tid+"'>"+data[i].name+"</label>";
    				}
    				$("#roles").append(add);
    			}
    		});
    }
    
    
    function selectRole(officeId){
    	$("#role_id1").empty();
    	var add="<option>请选择</option>";
		$.ajax({
			type:"post",
			url:"<%=path%>/sysuser/toSelectRole",
			data:{officeId:officeId},
			success:function (data){
				for(var i=0;i<data.length;i++){
					add+="<option value='"+data[i].tid+"'>"+data[i].name+"</option>";
				}
				$("#role_id1").append(add);
			}
		});
		 
    }
    
     var table;
    $(document).ready(function() {
        table = $("#gadtable").DataTable( {
            "pagingType": "simple_numbers",//设置分页控件的模式
            searching: false,//屏蔽datatales的查询框
            aLengthMenu:[15],//设置一页展示10条记录
            "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
            ordering:false,
            "oLanguage": {  //对表格国际化
                "sLengthMenu": "每页显示 _MENU_条",
                "sZeroRecords": "无匹配结果，请更换筛选条件再试",
                //  "sProcessing": "&lt;img src=’./loading.gif’ /&gt;",
                "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
                "sInfoEmpty": "当前第 0 - 0 条　共计 0 条",
                "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
                "sSearch": "搜索：",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                }
            },
            //"processing": true, //打开数据加载时的等待效果
            "serverSide": true,//打开后台分页
            "ajax": {
                "url": "/sysuser/queryTableData",
                "dataSrc": "aaData",
                "type":"post",
                "data": function (params) {
                    var username = $('#username').val();
                    var status = $('#status').val();
                    var officeId = $('#office_id').val();
                    //添加额外的参数传给服务器
                    params.username = username;
                    params.realName = username;
                    params.position = username;
                    params.status = status;
                    params.officeId = officeId;
                }
            },
            "columns": [
                { "bSortable": false,"data": "tid","defaultContent": '--'},
                { "data": "officeName","bSortable": false,"defaultContent": '--'},
                { "data": "roleName","bSortable": false,"defaultContent": '--'},
                { "data": "username" ,"bSortable": false,"defaultContent": '--'},//"bSortable",默认为true,表示是否在某一列上开启排序
                { "data": "jobNumber" ,"bSortable": false,"defaultContent": '--'},
                { "data": "phone","bSortable": false,"defaultContent": '--'},
                { "data": "status" ,"bSortable": false,"defaultContent": '--'}
            ],
            "columnDefs" : [
              {
                // 定义操作列,######以下是重点########
                "targets" : 7,//是操作按钮目标列
                "data" : null,
                "render" : function(data, type,row) {
                    var id = '"' + row.tid + '"';
                    var jobNumber = '"' + row.jobNumber + '"';
                    if(row.status==0){
                    	var html ="<a href='#tab4' data-toggle='tab' onclick='toedit(" + id + ")' class='btn-link'>编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                       html += "<a href='#modal-dialog3' onclick='getId("+id+")' data-toggle='modal' class='btn-link'>禁用</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                       html += "<a href='#modal-dialog2' onclick='getId("+ id +","+ jobNumber +")' class='btn-link' data-toggle='modal'>重置密码</a>"
                    }else{
                    	var html ="<a href='#modal-dialog4' onclick='getId("+id+")' data-toggle='modal' class='btn-link'>启用</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                    	html += "<a href='#modal-dialog2' onclick='getId("+id+","+ jobNumber +")' class='btn-link' data-toggle='modal'>重置密码</a>"
                    }
                    return html;
                }
            },
            {
                "targets" : 6,//是操作按钮目标列
                "data" : null,
                "render" : function(data, type,row) {
                    //在columnDefs中,data取值和row是一样的
                    if(row.status==0){
                    	var message = "使用中";
                    }else{
                    	var message = "已禁用";
                    }
                    return message;
                }
            } ]

        } );
    } );
    function search(){
        table.ajax.reload();
    }
    
    var externalId;
    var externalTable;
    $(document).ready(function (){
    	externalTable=$("#external").DataTable({
    		"pagingType": "simple_numbers",//设置分页控件的模式
            searching: false,//屏蔽datatales的查询框
            aLengthMenu:[15],//设置一页展示10条记录
            "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
            ordering:false,
            "oLanguage": {  //对表格国际化
                "sLengthMenu": "每页显示 _MENU_条",
                "sZeroRecords": "无匹配结果，请更换筛选条件再试",
                //  "sProcessing": "&lt;img src=’./loading.gif’ /&gt;",
                "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
                "sInfoEmpty": "当前第 0 - 0 条　共计 0 条",
                "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
                "sSearch": "搜索：",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                }
            },
            "serverSide": true,//打开后台分页
            "ajax": {
                "url": "/sysuser/selectTableData",
                "dataSrc": "aaData",
                "type":"post",
                "data": function (params) {
                    var channelId = $("#channelId").val();
                    var status = $("#status2").val();
                    params.channelId = channelId;
                    params.status = status;
                }
            },
            "columns": [
                        { "bSortable": false,"data": "tid","defaultContent": '--'},
                        { "data": "jobNumber","bSortable": false,"defaultContent": '--'},
                        { "data": "companyRegisterName" ,"bSortable": false,"defaultContent": '--'},//"bSortable",默认为true,表示是否在某一列上开启排序
                        { "data": "realName" ,"bSortable": false,"defaultContent": '--'},
                        { "data": "phone","bSortable": false,"defaultContent": '--'},
                        { "data": "status" ,"bSortable": false,"defaultContent": '--'}
                  ],
                  "columnDefs" : [ {
                      // 定义操作列,######以下是重点########
                      "targets" : 6,//是操作按钮目标列
                      "data" : null,
                      "render" : function(data, type,row) {
                          var id = '"' + row.tid + '"';
                          if(row.status==0){
                          	var html ="<a href='#modal-dialog' onclick='getId("+id+")' class='btn-link' data-toggle='modal'>重置密码</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                             html += "<a href='#modal-dialog3' onclick='getId("+id+")' data-toggle='modal' class='btn-link'>禁用</a>"
                          }else{
                          	var html ="<a href='#modal-dialog' onclick='getId("+id+")' class='btn-link' data-toggle='modal'>重置密码</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                          	html +="<a href='#modal-dialog4' onclick='getId("+id+")' data-toggle='modal' class='btn-link'>启用</a>"
                          }
                          return html;
                      }
                 },
                {
                     "targets" : 2,//是操作按钮目标列
                     "data" : null,
                     "render" : function(data, type,row) {
                         //在columnDefs中,data取值和row是一样的
                         if(row.channelType==1){
                         	var message = row.accountName;
                         }else{
                         	var message = row.companyRegisterName;
                         }
                         return message;
                     }
                 },
                 {
                     "targets" : 5,//是操作按钮目标列
                     "data" : null,
                     "render" : function(data, type,row) {
                         //在columnDefs中,data取值和row是一样的
                         if(row.status==0){
                         	var message = "使用中";
                         }else{
                         	var message = "已禁用";
                         }
                         return message;
                     }
                 }]
    	});
    });
    
    function searchExternal(){
    	externalTable.ajax.reload();
    }
</script>
</body>
</html>